<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/30
 * Time: 10:10
 */

namespace Admin\Controller;


use Api\Vendor\NetWarehourse\WHCancelOrder;
use Api\Vendor\NetWarehourse\WHCancelPurchaseOrder;
use Api\Vendor\NetWarehourse\WHOrderNotify;
use Api\Vendor\NetWarehourse\WHOrderPackage;
use Api\Vendor\NetWarehourse\WHOrderStatus;
use Library\Api\NetwhUploadHisApi;
use Library\Model\NetwhUploadHisModel;
use Shop\Api\OrderBillResultApi;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersExpressApi;
use Shop\Api\OrderStatusApi;
use Shop\Api\OrderStatusHistoryApi;
use Shop\Api\StockHisApi;
use Shop\Api\WMSOrderStatusApi;
use Shop\Model\OrdersModel;
use Shop\Model\OrderStatusHistoryModel;

class NetWHController extends AdminController{

    public function index(){

        $order_code = $this->_get("order_code","","缺少订单编号!");

        if(IS_GET){

            $map = array(
                'order_code'=>$order_code,
            );

            $result = apiCall(OrdersApi::GET_INFO,array($map));
            $this->checkApiCallResult($result);
            $this->assign("order",$result['info']);
//            $map['order_code'] = $result['info']['pay_code'];
            $map['type'] = NetwhUploadHisModel::UPLOAD_NET_WAREHOURSE_ORDER_NOTIFY;
            $page = array('curpage'=>I('get.p',0),'size'=>10);

            $result = apiCall(NetwhUploadHisApi::QUERY,array($map,$page,"upload_time desc"));

            $this->assign("list",$result['info']['list']);
            $this->assign("show",$result['info']['show']);

            $map = array(
                'order_code'=>$order_code,
                'status_type'=>OrderStatusHistoryModel::STATUS_TYPE_WMS,
            );

            $result = apiCall(OrderStatusHistoryApi::QUERY_NO_PAGING,array($map));

            $history = $result['info'];
            if(!empty($history)){
                foreach($history as $vo){
                    if(strtoupper($vo['status_type']) == strtoupper(OrderStatusHistoryModel::STATUS_TYPE_WMS)){
                        if($vo['next_status'] == WHOrderStatus::WMS_PRINT
                         || $vo['cur_status'] == WHOrderStatus::WMS_PRINT){
                            $this->assign("cant_cancel",1);
                        }
                        if($vo['next_status'] == WHOrderStatus::WMS_SHIPED){
                            $this->assign("shipped",1);
                        }
                    }
                }
            }
            $this->assign("history",$history);

            $result = apiCall(OrdersExpressApi::GET_INFO,array(array('order_code'=>$order_code)));

            if($result['status'] && is_array($result['info'])){

                $result = apiCall(OrderBillResultApi::QUERY_NO_PAGING,array(array('express_no'=>$express_no)));
                if($result['status']){
                    $this->assign("bill_list",$result['info']);
                }
            }



            $this->display();

        }else{

            $request = new WHOrderNotify();

            $result = $request->request($order_code);

            if($result['status'] == 0){
                $this->success($result['info']);
            }else{
                $this->error($result['info']);
            }

        }
    }

    public function package(){

        $order_code = $this->_get("order_code","","缺少订单编号!");

        if(IS_POST){
            $request = new WHOrderPackage();

            $result = $request->request($order_code);

            if($result['status'] == 0){
                $this->success($result['info']);
            }else{
                $this->error($result['info']);
            }
        }
    }

    public function cancel(){

        $order_code = $this->_get("order_code","","缺少订单编号!");

        if(IS_POST){

            $result = apiCall(OrdersApi::GET_INFO,array(array('order_code'=>$order_code)));
            $order_info = false;
            if($result['status'] && is_array($result['info'])){
                $order_info = $result['info'];
            }

            if(empty($order_info)){
                $this->error("获取订单信息失败!");
            }

            if(empty($order_info['pay_code'])){
                $this->error("该订单尚未支付!");
            }

            $request = new WHCancelOrder();

            $result = $request->request($order_info['pay_code']);

            if($result['status'] == 0){
                WMSOrderStatusApi::
                $this->success($result['info']);
            }else{
                $this->error($result['info']);
            }
        }

    }

    public function cancelPurchse(){
        $code = $this->_get('code','缺少入库单');


        $result = apiCall(OrdersApi::GET_INFO,array(array('order_code'=>$code)));
        $order_info = false;
        if($result['status'] && is_array($result['info'])){
            $order_info = $result['info'];
        }

        if(empty($order_info)){
            $this->error("获取订单信息失败!");
        }

        if(empty($order_info['pay_code'])){
            $this->error("该订单尚未支付!");
        }

        $request = new WHCancelPurchaseOrder();

        $result =   $request->request($order_info['pay_code']);

        apiCall(StockHisApi::SAVE,array(array('in_storage_code'=>$order_info['pay_code']),array('cancel_status'=>1)));


        if($result['status'] == 0){
            $this->success($result['info']);
        }else{
            $this->error($result['info']);
        }



    }

}